# -*- coding: utf-8 -*-
# created on 2016/11/30
# 

from mathsolver.functions.base import *
from mathsolver.functions.base.base import new_latex
from mathsolver.functions.fushu.base import Complex
from sympy import fraction, together
from mathsolver.functions.fushu.basic import fs_search_multiple


class FSNormalized001(BaseFunction):
    """
    复数\\frac{-4i}{1+\\sqrt{3}i}的虚部是()
    """
    def solver(self, *args):
        assert len(args) == 1
        assert isinstance(args[0], BaseComplexPoly) or isinstance(args[0], BaseComplexValue) or isinstance(args[0], BaseComplex)
        if isinstance(args[0], BaseComplexValue):
            poly = args[0].sympify()
            poly = poly[list(poly.keys())[0]][0]
        else:
            poly = args[0].sympify()
        new_poly = poly
        new_poly = new_poly.expand().simplify()
        new_poly_symbols = new_poly.free_symbols
        if new_poly_symbols:
            known = self.known
            answers = fs_search_multiple(known, new_poly)
            if answers:
                for answer in answers:
                    new_poly = new_poly.subs(answer)
        new_poly = together(new_poly)
        fenzhi, fenmu = fraction(new_poly)
        assert str(fenmu).find(str("I")) < 0
        com = Complex(new_poly)
        norm = com.comPoly
        self.steps.append(["", "%s = %s" % (new_latex(poly), Complex.print_complex(norm))])
        self.output.append(BaseValue(norm))
        self.label.add("复数的四则运算")
        return self


class FSNormalized002(BaseFunction):
    """
    设 a、b 、c 、d∈R ,若\\frac{{a + bi}}{{c + di}} 为实数,则( )
    :sympy化简后复数的分母还有I的情况
    """
    def solver(self, *args):
        assert len(args) == 1
        assert isinstance(args[0], BaseComplexPoly)
        poly = args[0].sympify()
        new_poly = poly
        new_poly = new_poly.expand().simplify()
        new_poly = together(new_poly)
        numerator, denominator = fraction(new_poly)
        assert str(denominator).find(str("I")) >= 0
        denominator_com = Complex(denominator)
        denominator_conjugate = denominator_com.conjugate
        new_denominator = denominator * denominator_conjugate
        new_denominator = new_denominator.expand().simplify()
        new_numerator = numerator * denominator_conjugate
        new_numerator = new_numerator.expand().simplify()
        new_poly = new_numerator / new_denominator
        com = Complex(new_poly)
        norm = com.comPoly
        self.steps.append(["", "%s = %s" % (new_latex(poly), Complex.print_complex(norm))])
        self.output.append(BaseValue(norm))
        self.label.add("复数的四则运算")
        return self


class FSNormalized(BaseFunction):
    CLS = [FSNormalized001, FSNormalized002]

    def solver(self, *args):
        known = self.known
        r = None
        for cl in FSNormalized.CLS:
            try:
                new_known = dict(known)
                r = cl(known=new_known, verbose=True).solver(*args)
                break
            except Exception:
                pass
        if not r:
            raise 'try fail'
        return r
